<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use think\Exception;

class Account extends Base
{
    public function index()
    {
        if (request()->isAjax()) {

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (isset($param['searchText']) && !empty($param['searchText'])) {
                $where['name'] = ['like', '%' . $param['searchText'] . '%'];
            }

            $selectResult = db('account')->where($where)->limit($offset, $limit)->order('id desc')->select();

            foreach ($selectResult as $key => $vo) {
                if (in_array('account/accountimport', session('action'))) {
                    $operate = [
                        '导入价格' => url('account/accountimport', ['id' => $vo['id']]),
                        '编辑' => url('account/accountEdit', ['id' => $vo['id']]),
                        '删除' => "javascript:roleDel('" . $vo['id'] . "')"
                    ];
                } else {
                    $operate = [
                        '编辑' => url('account/accountEdit', ['id' => $vo['id']]),
                        '删除' => "javascript:roleDel('" . $vo['id'] . "')"
                    ];
                }

                $selectResult[$key]['operate'] = showOperate($operate);

            }

            $return['total'] = db('account')->where($where)->count();  //总数据
            $return['rows'] = $selectResult;

            return json($return);
        }

        return $this->fetch();
    }

    //添加账号
    public function accountAdd()
    {
        if (request()->isPost()) {

            $wxname = input('wxname');
            $wxid = input('wxid');
            $wxinfo = input('wxinfo');
            $wxfans = input('wxfans');
            $wxread = input('wxread');
            $wxcontact = input('wxcontact');
            $wxtype = input('wxtype');
            $area = input('area');

            $file = request()->file('wxlogo');
            $info = $file->move(ROOT_PATH . 'public/upload');
            if ($info) {
                // 成功上传后 获取上传信息
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $wxlogo = $info->getSaveName();
            } else {
                // 上传失败获取错误信息
                $wxlogo = '';
            }

            $ok = db('account')->insert(array('type' => 1, 'name' => $wxname, 'logo' => $wxlogo, 'wechat_id' => $wxid, 'info' => $wxinfo, 'fans' => $wxfans, 'read_count' => $wxread, 'contact' => $wxcontact, 'created_time' => date('Y-m-d H:i:s'), 'classified' => $wxtype, 'area' => $area));

            return json(['code' => $ok, 'data' => '', 'msg' => $ok == 1 ? '成功' : '失败']);
        }

        return $this->fetch();
    }

    public function accountEdit()
    {
        if (request()->isPost()) {
            $wxname = input('wxname');
            $wxid = input('wxid');
            $wxinfo = input('wxinfo');
            $wxfans = input('wxfans');
            $wxread = input('wxread');
            $wxcontact = input('wxcontact');
            $wxlogo = input('wxlogo');
            $wxtype = input('wxtype');
            $area = input('area');

            $ok = db('account')->where('id', input('edit_id'))->update(array('type' => 1, 'name' => $wxname, 'logo' => $wxlogo, 'wechat_id' => $wxid, 'info' => $wxinfo, 'fans' => $wxfans, 'read_count' => $wxread, 'contact' => $wxcontact, 'classified' => $wxtype, 'area' => $area));

            return json(['code' => $ok, 'data' => '', 'msg' => $ok == 1 ? '成功' : '失败']);
        }

        $id = input('param.id');
        $account = db('account')->where('id', $id)->find();
        $this->assign([
            'account' => $account
        ]);
        return $this->fetch('accountedit_wx');
    }

    /**
     * 导入价格
     * @return string
     */
    public function accountImport()
    {
        if (request()->isPost()) {
            $file = request()->file('price');
            $info = $file->move(ROOT_PATH . 'price');
            if ($info) {
                // 成功上传后 获取上传信息
                try {
                    import("excel.PHPExcel.Reader.Excel2007");
                    $PHPReader = new \PHPExcel_Reader_Excel2007();
                    $PHPExcel = $PHPReader->load($info->getPathname());
                    $currentSheet = $PHPExcel->getSheet(0);

                    //获取总列数
                    $allColumn = $currentSheet->getHighestColumn();
                    //获取总行数
                    $allRow = $currentSheet->getHighestRow();

                    //循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
                    for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
                        //从哪列开始，A表示第一列
                        for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
                            //数据坐标
                            $address = $currentColumn . $currentRow;
                            //读取到的数据，保存到数组$arr中
//                        $arr[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
                            $arr[$currentColumn] = $currentSheet->getCell($address)->getValue();
                        }
                        $data[] = $arr;
                    }

                    $ok = db('account')->where('id', input('account_id'))->update(array('price' => json_encode($data), 'status' => 1));
                } catch (Exception $e) {
                    $ok = 0;
                }
            } else {
                // 上传失败获取错误信息
                $ok = 0;
            }
            return json(['code' => $ok, 'data' => '', 'msg' => $ok == 1 ? '成功' : '失败']);
        }
        $this->assign('account_id', input('param.id'));
        return $this->fetch();
    }

    //删除账号
    public function accountDel()
    {
        $id = input('param.id');

        try {
            db('account')->where('id', $id)->delete();
            $flag = ['code' => 1, 'data' => '', 'msg' => '删除角色成功'];
        } catch (PDOException $e) {
            $flag = ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }
}
